package com.wenhuitec.android.tang.ui.dialog;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONArray;
import com.dhkj.web.utils.MyObserverRes;
import com.dhkj.web.utils.RequestUtils;
import com.wenhuitec.android.tang.R;
import com.wenhuitec.android.tang.config.SystemInit;
import com.wenhuitec.android.tang.entity.DeviceCarModel;
import com.wenhuitec.android.tang.sysui.model.CarFoodsModel;
import com.wenhuitec.android.tang.ui.activity.AliFaceActivity;
import com.wenhuitec.android.tang.ui.activity.CardActivity;
import com.wenhuitec.android.tang.ui.activity.ScanQrActivity;
import com.wenhuitec.android.tang.util.BytesUtil;
import com.wenhuitec.android.tang.util.PrintUtil;
import com.wenhuitec.android.tang.util.UsbPrinter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class PaySelectDepotDialog extends AppCompatActivity {
    private Context mContext;
    private RecyclerView recyclerView;
    private UsbPrinter usbPrinter;
    private String posnum = "01";
    private String operaId = "01";
    private Button cardBut;
    private Button aliFaceBut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_pay_select);
        cardBut = findViewById(R.id.cardBut);
        cardBut.setVisibility(View.VISIBLE);

        SharedPreferences sharedPreferences =
                getSharedPreferences("systemI", Context.MODE_PRIVATE); //私有数据
        posnum = sharedPreferences.getString("user", "");
        operaId = sharedPreferences.getString("user", "");
        mContext = this;
        usbPrinter = UsbPrinter.getInstance(this);
        String data = this.getIntent().getExtras().getString("data");
        List<CarFoodsModel> carFoodsModels = JSONArray.parseArray(data, CarFoodsModel.class);
        if (carFoodsModels.size() == 0) {
            Toast.makeText(mContext, "请选择购买商品", Toast.LENGTH_LONG).show();
            finish();
            return;
        }

        //将购物车分组
        Map<Integer, List<CarFoodsModel>> carFoodsMap = carFoodsModels.stream().collect(Collectors.groupingBy(CarFoodsModel::getAddr));
        List<DeviceCarModel> deviceCarModels = new ArrayList<>();

        for (Integer goo : carFoodsMap.keySet()) {
            DeviceCarModel deviceCarModel = new DeviceCarModel();
            deviceCarModel.setCarFoods(carFoodsMap.get(goo));
            deviceCarModel.setPosNum(posnum);
            deviceCarModel.setOperaId(posnum);
            deviceCarModel.setAddr(carFoodsMap.get(goo).get(0).getAddr());
            deviceCarModels.add(deviceCarModel);
        }

        findViewById(R.id.scanQrBut).setOnClickListener(v -> {
            Intent intent = new Intent(mContext, ScanQrActivity.class);
            intent.putExtra("data", JSONArray.toJSONString(deviceCarModels));
            startActivity(intent);
            setResult(RESULT_OK);
            finish();
        });

        cardBut.setOnClickListener(view -> {
            CardActivity.startAct(mContext, JSONArray.toJSONString(deviceCarModels));
            setResult(RESULT_OK);
            finish();
        });

        findViewById(R.id.cashBut).setVisibility(View.GONE);
        findViewById(R.id.cashBut).setOnClickListener(view -> {
            //提交给服务器，服务器接收成功后则返回是否打印消息
            RequestUtils.depotCashPayBatch(mContext, new MyObserverRes<String>(mContext) {
                        @Override
                        public void onSuccess(String result) {
                            System.out.println(result);
                            List<DeviceCarModel> deviceCarModelList = JSONArray.parseArray(result, DeviceCarModel.class);
                            for (DeviceCarModel goo : deviceCarModelList) {
                                try {
                                    PrintUtil.setPrintDevice(SystemInit.getInstance(mContext).getPrintDeviceType());
                                    byte[] printByte = BytesUtil.printOrder(goo);
                                    if (printByte != null) {
                                        try {
                                            usbPrinter.write(printByte);
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            setResult(RESULT_OK);
                            finish();
                        }

                        @Override
                        public void onFailure(Throwable e, String errorMsg) {
                            finish();
                        }
                    }, JSONArray.toJSONString(deviceCarModels)
            );
        });

        aliFaceBut = findViewById(R.id.aliFaceBut);
        aliFaceBut.setOnClickListener(view -> {
            Intent intent = new Intent(mContext, AliFaceActivity.class);
            intent.putExtra("data", JSONArray.toJSONString(deviceCarModels));
            startActivity(intent);
            setResult(RESULT_OK);
            finish();
        });
    }
}
